//
//  FeedbackViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/9/2.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "FeedbackViewController.h"
#import "SocketRequest.h"
#import "Singleton.h"

#define TextViewLength      500

@interface FeedbackViewController ()<SocketRequestDelegate> {
    SocketRequest *_socketRequest;
}

@end

@implementation FeedbackViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"意见反馈";
        _socketRequest = [[SocketRequest alloc] initWithSocketRequestDelegate:self];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.    
    [self rightBarButtonItemTitle:@"发表" Method:@selector(submitFeedback)];
}

- (void)backLastView {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)submitFeedback {
    [self.view endEditing:YES];
    if (_contentTextView.text.length <= 0) {
        [DSToast showText:@"请输入你的意见" InView:self.view];
    } else {
        [ToastActivity makeActivity:@"意见反馈中..." InView:self.view];
        NSMutableData *mutableData = [[NSMutableData alloc] initWithData:[SocketMethod socketHeadDataMethod:@"STO1"]];
        [mutableData appendBytes:[SocketMethod checkLegal:[Singleton shareInstance].userMessage.userId] length:64];
        NSData *contentData = [_contentTextView.text dataUsingEncoding:NSUTF8StringEncoding];
        [mutableData appendData:[SocketMethod hexDataWithLength:contentData.length]];
        [mutableData appendData:contentData];
        [_socketRequest addSocketData:mutableData SocketTag:1];
    }
}

#pragma mark SocketRequestDelegate
- (void)socket:(GCDAsyncSocket *)socket Code:(NSString *)code Msg:(NSString *)msg Data:(NSData *)data {
    if ([code isEqualToString:SuccessSocket]) {
        [ToastActivity makeSuccess:@"意见反馈成功" InView:self.view];
        [self performSelector:@selector(backLastView) withObject:nil afterDelay:1];
    } else {
        [ToastActivity makeError:msg InView:self.view];
    }
}

- (void)socket:(GCDAsyncSocket *)socket Msg:(NSString *)msg {
    [ToastActivity makeError:msg InView:self.view];
}

#pragma mark TouchMethod
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length <= 0) {
        _tipLabel.hidden = NO;
    } else {
        _tipLabel.hidden = YES;
    }
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    if (!position) {
        [self textViewText:textView.text];
    }
}

- (void)textViewText:(NSString *)text {
    if (text.length > TextViewLength) {
        _contentTextView.text  = [text substringToIndex:TextViewLength];
        [_contentTextView resignFirstResponder];
    }
    _numberLabel.text = [NSString stringWithFormat:@"%d/%d",(int)_contentTextView.text.length,TextViewLength];
}

- (void)dealloc {
    _socketRequest = nil;
}

@end
